package com.cnzz.site1255174697.activity.taobao;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.factory.GoodsFactory;
import com.cnzz.site1255174697.holder.GoodsListHolder;
import com.cnzz.site1255174697.model.MallEntity;
import com.cqyanyu.yanyu.utils.XToastUtil;
import com.cqyanyu.yanyu.view.recyclerView.YLoadMoreHolder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 搜索界面
 */
public class SearchActivity extends SearchRecyclerActivity implements TextWatcher {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindItemHolder(MallEntity.DataBean.ResultsBean.AitaobaoItemBean.class, GoodsListHolder.class);
        mRecyclerView.setSpanCount(2);
        mRecyclerView.setCellSpanCount(YLoadMoreHolder.class, 2);
        initView();
        loadData();
    }

    private void initView() {
        setBtn_rightText(getString(R.string.cancle));
        etSearch.addTextChangedListener(this);
    }

    public void loadData() {
        JSONArray localJSONArray = new JSONArray();
        JSONObject localJSONObject1 = new JSONObject();
        JSONObject localJSONObject2 = new JSONObject();
        JSONObject localJSONObject3 = new JSONObject();
        JSONObject localJSONObject4 = new JSONObject();
        try {
//            localJSONObject1.put("fields", "open_iid,title,nick,pic_url,price,end_volume,commission,commission_rate,commission_num,commission_volume,seller_credit_score,item_location,volume,coupon_price,coupon_rate,coupon_start_time,coupon_end_time,shop_type");
            localJSONObject1.put("fields","num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");

            localJSONObject2.put("keyword", etSearch.getText().toString().equals("")?"女装":etSearch.getText().toString());
            localJSONObject3.put("page_no", page + "");
            localJSONObject4.put("sort", "tk_rate");
            localJSONArray.put(localJSONObject1);
            localJSONArray.put(localJSONObject2);
            localJSONArray.put(localJSONObject3);
            localJSONArray.put(localJSONObject4);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GoodsFactory.getGoods(this, "tbk.item.get", localJSONArray.toString(), page, this);
    }

    public void onBtnClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left:
                finish();
                InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputmanger.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
                break;
            case R.id.btn_right:
                if (!TextUtils.isEmpty(etSearch.getText().toString())) {
                    mRecyclerView.onRefresh();
                    loadData();
                } else {
                    XToastUtil.showToast(this, "请输入搜索内容");
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        boolean isAll = etSearch.getText().toString().equals("");
        if (isAll == false) {
            btn_right.setText(getString(R.string.search));
        } else {
            btn_right.setText(getString(R.string.cancle));
        }
    }
}
